package com.weixin.demo.kotlinboot.controllers

import com.alibaba.fastjson.JSONObject
import com.weixin.demo.kotlinboot.bean.Video
import com.weixin.demo.kotlinboot.dao.VideoRepository
import com.weixin.demo.kotlinboot.util.UploadImage
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.MediaType
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.*
import java.io.IOException
import java.io.FileOutputStream
import java.io.FileInputStream
import org.springframework.web.multipart.MultipartHttpServletRequest
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.multipart.MultipartFile
import java.io.File
import javax.servlet.http.HttpServletRequest
import java.util.HashMap


/**
 * Created by liuwill on 2017/3/15.
 */
@RestController
@RequestMapping(value = "/video")
class DataController {
    @Autowired
    lateinit var video: VideoRepository

    @GetMapping("/getVideoList")
    fun getVideo(model: Model): String {
        val videolist = video.findAll()
        return JSONObject.toJSONString(videolist).toString()
    }


    @RequestMapping(value = "", method = arrayOf(RequestMethod.GET, RequestMethod.PUT))
    fun index(): Map<String, Any> {
        val resultMap = HashMap<String, Any>()

        resultMap["status"] = true
        resultMap["code"] = "100010"
        resultMap["msg"] = "success"
        resultMap["data"] = "data"
        return resultMap
    }

    @RequestMapping(value = "show", method = arrayOf(RequestMethod.PUT), consumes = arrayOf(MediaType.APPLICATION_JSON_UTF8_VALUE))
    fun test(@RequestBody reqMap: Map<Any, Any>): Map<Any, Any> {
        return reqMap
    }

    @RequestMapping(value = "/addClick/{id}", method = arrayOf(RequestMethod.GET))
    fun add(model: Model, @PathVariable("id") id: Long): String {

        var v: Video = video.findById(id)

        v.chick += 1
        val result = video.save(v)
        return JSONObject.toJSONString(result).toString()
    }

    @RequestMapping(value = "/addVideo", method = arrayOf(RequestMethod.POST))
    fun addVideo(@RequestBody reqMap: Map<Any, Any>): String {
        var v = Video()
        v.videoname = reqMap.get("title").toString()
        v.author = reqMap.get("author").toString()
        v.videourl = reqMap.get("videou").toString()
        val result = video.save(v)
        return JSONObject.toJSONString(result).toString()
    }

    @RequestMapping(value = "/testUploadFile", method = arrayOf(RequestMethod.POST))
    fun testUploadFile(req: HttpServletRequest,
                       @RequestParam(value = "file", required = false) multipartFile: MultipartFile): String {
        val fileName = multipartFile.getOriginalFilename()
        val uplodaImage = UploadImage()
        val map = HashMap<String, String>()
        val systemName = System.getProperties().getProperty("os.name")
        println(systemName)
        var realPath = ""
        if (systemName.startsWith("mac") || systemName.startsWith("Mac")) {
            realPath = this.javaClass.getResource("/").path
        }
        if (systemName.startsWith("Linux") || systemName.startsWith("linux")) {
            realPath = "/opt/luway/img/"
        }
        // 自定义的文件名称uplodaImage.upload(multipartFile);
        val trueFileName = System.currentTimeMillis().toString() + fileName
        // 设置存放图片文件的路径
        val path = realPath + trueFileName
        multipartFile.transferTo(File(path))
        println("## ship ## 路径为：" + path)
        // 上传至七牛云空间
        uplodaImage.upload(path, trueFileName)
        // 删除本地
        val file = File(path)
        file.delete()
        // 远程文件路径名称
        val newPath = "http://opvmfbxz5.bkt.clouddn.com/" + trueFileName
        map.put("code", "0")
        map.put("msg", "上传成功")
        map.put("data", newPath)
        return JSONObject.toJSONString(map)
    }

}